# encoding: utf-8
"""
@author: 夏洛
@QQ: 1972386194
@file: 04-httpx_test.py
"""
import httpx

headers = {'user-agent': 'my-app/1.0.0'}
params = {'key1': 'value1', 'key2': 'value2'}
url = 'https://httpbin.org/get'
r = httpx.get(url, headers=headers, params=params)

res = httpx.get('https://img2.baidu.com/it/u=1657873467,1784376830&fm=253&fmt=auto&app=138&f=JPEG?w=450&h=438')

import httpx
import os

class S_wm(object):
    def __init__(self):
        self.headers = {
                'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36'
            }

    def get_url_list(self):
        url_list = ['https://img.vm.laomishuo.com/image/2019/12/2019122210294290-scaled.jpeg',
                    'https://img1.baidu.com/it/u=3980896846,3728494487&fm=253&fmt=auto&app=138&f=JPEG?w=333&h=499',
                    'https://img1.baidu.com/it/u=467548803,2897629727&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500',
                    ]
        return url_list


    def save_image(self,filename,img):
        with open(filename, 'wb') as f:
            f.write(img.content)
        print('图片提取成功')


    def run(self):
        url_list = self.get_url_list()
        for index,u in enumerate(url_list):
            file_name = './image/{}.jpg'.format(index)
            # httpx.get(url=u)
            data = httpx.request('get', u, headers=self.headers)
            self.save_image(file_name,data)

'''
httpx.request('get')  = httpx.get()

建议使用3.7 3.8 等解释器
'''

if __name__ == '__main__':
    url = 'https://www.vmgirls.com/13344.html'
    s = S_wm()
    if os.path.exists("./image") is False:
        os.mkdir('./image')
    s.run()
